package cn.tedu.sp08.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp08.feign.ItemClient;
import cn.tedu.sp08.feign.OrderClient;
import cn.tedu.sp08.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 集成Ribbon:->RestTemplate,负载均衡,重试
 *  负载均衡和重试是默认的,不需要有配置.关闭重试就两个参数设置为0
 *  重试的默认参数:
 *      MaxAutoRetries=0
 *      MaxAutoRetriesNextServer=1
 *      ReadTimeout=1000
 *      修改的话在yml中修改
----------------------------------------------------------------------------------------
 * 集成Hystrix:->降级,熔断
 *  默认不启用,不推荐启用
 *  配置降级:
 *      1.添加Hystrix,actuator依赖
 *      2.@EnableCircuitBreaker
 *      3.yml启用Hystrix->feign.hystrix.enabled=true
 *      4.yml中暴露监控端点management.endpoints.web.exposure.include=hystrix.stream
 *      5.添加降级类(实现该接口)代码,@Component
 *  熔断:不需要配置,只要达到条件就会自动打开
 */
@RestController
@Slf4j
public class FeignController {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderClient orderClient;

    @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        //rt.getForObject("http://item-service/{1}",...,...)
        //通过声明式客户端接口,调用远程商品服务的获取商品列表功能
        return itemClient.getItems(orderId);
    }
    @PostMapping("/item-service/decreaseNumber")
    JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        return itemClient.decreaseNumber(items);
    }

    @GetMapping("/user-service/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return userClient.getUser(userId);
    }
    @GetMapping("/user-service/{userId}/score")
    public JsonResult<?> addScore(@PathVariable Integer userId,@RequestParam Integer scoreId){
        return userClient.addScore(userId, scoreId);
    }
    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return orderClient.getOrder(orderId);
    }
    @GetMapping("/order-service")
    public JsonResult<?> addOrder(){
        return orderClient.addOrder();
    }

}
